/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.SejourController;
import Model.Agenda;
import Model.Sejour;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

/**
 *
 * @author Hugo
 */
public class ModifierSejour extends AbstractViewPanel {

    /**
     * Creates new form ModifierSejour
     */
    private SejourController controller;
    
    public ModifierSejour(SejourController controller) {
        initComponents();
        this.controller = controller;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtNum = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnChercher = new javax.swing.JButton();

        jLabel1.setText("Numéro de séjour:");

        btnChercher.setText("Chercher");
        btnChercher.setToolTipText("");
        btnChercher.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnChercherMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtNum, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(btnChercher)))
                .addContainerGap(38, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(btnChercher)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnChercherMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnChercherMouseClicked
        try{
            Sejour sejour = Agenda.getInstance().TrouverSejour(Integer.parseInt(this.txtNum.getText()));
            if(sejour != null){
                ChangerChambre vueChangerSejour = new ChangerChambre(controller, sejour);
                controller.addModel(sejour);
                controller.addView(vueChangerSejour);

                JFrame displayFrame = new JFrame("Changer une chambre");
                displayFrame.getContentPane().add(vueChangerSejour, BorderLayout.CENTER);
                displayFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                displayFrame.pack();
                displayFrame.setVisible(true);
                
                this.getTopLevelAncestor().setVisible(false);
                //controller.updateListeChambre();
                
            }
        }
        catch( NumberFormatException e){
            JOptionPane.showMessageDialog(this, "Vous dever entrer un entier comme numéro de séjour.");
        }
        
    }//GEN-LAST:event_btnChercherMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChercher;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtNum;
    // End of variables declaration//GEN-END:variables
    
    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
         switch(evt.getPropertyName()){
            case SejourController.CHANGE_CHAMBRE_PROPERTY:
                this.getTopLevelAncestor().setVisible(false);
                break;
        }
    }
}
